package com.youlu.campus.web.feedback.service.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.OrderInfo;
import com.youlu.campus.service.base.BaseService;
import com.youlu.campus.web.feedback.entity.FeedBackInfo;
import com.youlu.campus.web.feedback.service.FeedBackService;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.web.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class FeedBackServiceImpl extends BaseService implements FeedBackService {
    @Autowired
    private UserService userService;

    @Override
    public boolean create(FeedBackInfo info, UserPO userPO) {
        log.info(":>>> 开始创建问题反馈:{}", JSON.toJSONString(info));
        if (Objects.isNull(info) || StringUtils.isBlank(info.getUserId())
                || (StringUtils.isBlank(info.getContent()) && CollectionUtils.isEmpty(info.getImageUrls()))) {
            log.error(":>>> 创建问题反馈参数为空");
            throw new BusinessException("参数错误!");
        }
        if (StringUtils.isBlank(info.getMobile())) {
            log.error(":>>> 问题反馈手机号不能为空");
            throw new BusinessException("问题反馈手机号不能为空!");
        }
        if (StringUtils.isBlank(info.getName())) {
            log.error(":>>> 问题反馈姓名不能为空");
            throw new BusinessException("问题反馈姓名不能为空!");
        }
        /**
         * 状态,0:待付款,1:已付款,2:制作中,3:待发货,4:待收货,5:已收货,6:订单完成,7:订单关闭,8:售后中,9:部分已发货
         */
        String mobile = info.getMobile();
        Date date = new Date();
        info.setUserHeadImageUrl(userPO.getAvatarUrl());
        info.setUserNickName(userPO.getNickName());
        info.setTenantId(defaultTenantId);
        info.setCreatedTime(date);
        info.setUserGender(userPO.getGender());
        info.setUserNickName(userPO.getName());
        // 待处理
        info.setStatus(0);
        info.setUniversityId(userPO.getUniversityId());
        info.setUniversityName(userPO.getUniversityName());
        info.setName(info.getName());
        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(info.getName())) {
            List<String> statuses = new ArrayList<>();
            statuses.add("1");
            statuses.add("4");
            statuses.add("9");
            Query query = new Query().addCriteria(Criteria.where("accountInfoId").is(userPO.getId()).and("status").in(statuses));
            OrderInfo orderInfo = mongoTemplate.findOne(query, OrderInfo.class);
            if (StringUtils.isBlank(mobile)) {
                mobile = orderInfo.getMobile();
            }
            if (StringUtils.isBlank(info.getName())) {
                info.setName(orderInfo.getName());
            }
        }
        if (StringUtils.isBlank(mobile)) {
            mobile = userPO.getMobile();
        }
        info.setMobile(mobile);
        mongoTemplate.insert(info);
        return true;
    }

    @Override
    public boolean miniCreate(FeedBackInfo info, UserPO userPO) {
        log.info(":>>> 开始创建简版问题反馈:{}", JSON.toJSONString(info));
        if (Objects.isNull(info) || StringUtils.isBlank(info.getUserId())
                || (StringUtils.isBlank(info.getContent()) && CollectionUtils.isEmpty(info.getImageUrls()))) {
            log.error(":>>> 创建问题反馈参数为空");
            throw new BusinessException("参数错误!");
        }

        info.setUserHeadImageUrl(userPO.getAvatarUrl());
        info.setUserNickName(userPO.getNickName());
        info.setTenantId(defaultTenantId);
        info.setCreatedTime(new Date());
        info.setUserGender(userPO.getGender());
        info.setUserNickName(userPO.getName());
        // 待处理
        info.setStatus(0);
        info.setUniversityId(userPO.getUniversityId());
        info.setUniversityName(userPO.getUniversityName());
        mongoTemplate.insert(info);
        return true;
    }
}
